package com.sdwz.library.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.sdwz.library.entity.BookInfo;
import com.sdwz.library.entity.vo.BookExcelVo;
import com.sdwz.library.mapper.BookInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class ExcelListener extends AnalysisEventListener<BookExcelVo> {

    @Autowired
    private BookInfoMapper bookInfoMapper;

    /**
     * 如果使用了spring,请使用这个构造方法。每次创建Listener的时候需要把spring管理的类传进来
     *
     * @param
     */
    public ExcelListener(BookInfoMapper bookInfoMapper) {
        this.bookInfoMapper = bookInfoMapper;
    }

    @Override
    public void invoke(BookExcelVo bookExcelVo, AnalysisContext analysisContext) {
        BookInfo bookInfo = new BookInfo();
        BeanUtils.copyProperties(bookExcelVo,bookInfo);
//        bookInfo.setPublicationTime(LocalDate.parse(bookExcelVo.getPublicationTime(),DateTimeFormatter.ofPattern("yyyy.MM.dd")));
        System.out.println("========================="+bookInfo);
        bookInfoMapper.insert(bookInfo);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
